<?php
/**
 *  扩展字段模型类
 */
namespace app\admin\model;
use think\Model;
use think\Db;
class Extfieldm extends Model
{
	protected $name = 'extfield';
    // 获取扩展字段列表
    public function getList($UrlParam,$PageCount)
    {
		$Where=array();
		$fields = array(
            'a.*',
            'm.name'
        );
		if($UrlParam['mcode']){
			$Where['mcode']=$UrlParam['mcode'];
		}
         return $this->alias('a')
			->field($fields)
			->where($Where)
			->order('a.sorting asc,a.id asc')
			->join('model m', 'a.mcode=m.mcode', 'left')
			->paginate($PageCount,false,['query' => $UrlParam]);
    }
    // 查找扩展字段
    public function findExtField($field, $keyword)
    {
		$Where=array();
		if(!empty($UrlParam['keyword'])) $Where['description']=['like','%'.trim($UrlParam['keyword']).'%'];
        return $this->where($Where)
            ->order('sorting asc,id asc')
            ->select();
    }
    // 检查扩展字段
    public function checkExtField($name)
    {
        return $this->where("name",$name)->find();
    }
     // 获取模型字段
    public function getModelField($mcode)
    {
        return $this->where("mcode='$mcode'")
            ->order('sorting asc,id asc')
            ->select();
    }
    // 获取扩展字段详情
    public function getExtField($id)
    {
        return $this->where("id",$id)->find();
    }
    // 获取扩展字段名称
    public function getExtFieldName($id)
    {
        return $this->where("id",$id)->value('name');
    }

    // 添加扩展字段
    public function addExtField(array $data)
    {
        return $this->insert($data);
    }

    // 删除扩展字段
    public function delExtField($id)
    {
        return $this->where("id",$id)->delete();
    }

    // 修改扩展字段
    public function modExtField($id, $data)
    {
        return $this->where("id",$id)->update($data);
    }

    // 判断字段是否存在
    public function isExistField($field)
    {
        $fields = Db::getFields('cb_content_ext');
        if (in_array($field, $fields)) {
            return true;
        } else {
            return false;
        }
    }
	 // 判断字段是否存在
    public function amd($sqlstring)
    {
        $fields = Db::amd($sqlstring);
        if ($fields) {
            return true;
        } else {
            return false;
        }
    }
}